package cn.cleanarch.infra.idp.pms.config;

import cn.cleanarch.infra.idp.pms.service.DesignFileService;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 解压文件清理定时任务配置
 * 定期清理过期的解压文件，避免磁盘空间占用过大
 */
@Configuration
@EnableScheduling
public class ExtractCleanupConfig {
    
    @Autowired
    private DesignFileService designFileService;
    
    /**
     * 每天凌晨2点执行清理任务
     * 清理7天前解压的文件
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void cleanupExpiredExtractFiles() {
        try {
            // 7天的毫秒数
            long sevenDaysAgo = 7L * 24L * 60L * 60L * 1000L;
            designFileService.cleanupExpiredExtractFiles(sevenDaysAgo);
            System.out.println("过期解压文件清理任务执行完成");
        } catch (Exception e) {
            System.err.println("执行过期解压文件清理任务时发生错误: " + e.getMessage());
        }
    }
    
    /**
     * 每小时执行一次轻量级清理
     * 清理1天前解压的文件
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void lightCleanup() {
        try {
            // 1天的毫秒数
            long oneDayAgo = 24L * 60L * 60L * 1000L;
            designFileService.cleanupExpiredExtractFiles(oneDayAgo);
        } catch (Exception e) {
            System.err.println("执行轻量级清理任务时发生错误: " + e.getMessage());
        }
    }
}